package util

import (
	"fmt"
	"os"
	"path/filepath"
)

func FilePutContent(path string, content string, appended bool) error {
	flag := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
	if appended {
		flag = os.O_APPEND | os.O_WRONLY | os.O_CREATE
	}
	file, err := os.OpenFile(path, flag, 0644)
	if err != nil {
		fmt.Println("无法打开文件:", err)
		return err
	}
	defer file.Close()

	// 写入数据到文件
	data := []byte(content)
	_, err = file.Write(data)
	if err != nil {
		fmt.Println("写入文件时出错:", err)
		return err

	}
	return nil
}

// 返回绝对路径
func ScanDir(dir string) ([]string, error) {
	var files []string
	//方法一
	var walkFunc = func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			files = append(files, path)
		}
		//fmt.Printf("%s\n", path)
		return nil
	}
	err := filepath.Walk(dir, walkFunc)
	return files, err

}
